package com.nwpu.coursesupport.interceptor;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationListener;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nwpu.coursesupport.controller.UserController;
import com.nwpu.coursesupport.enumeration.RoleType;
import com.nwpu.coursesupport.realm.CustomizedToken;

public class CustomAuthenticationListener implements AuthenticationListener {

    private static Logger logger = LoggerFactory.getLogger(CustomAuthenticationListener.class);

	
	@Override
	public void onSuccess(AuthenticationToken token, AuthenticationInfo info) {
		CustomizedToken authcToken = (CustomizedToken)token; 
		Session session=getSession();
		session.setAttribute("username",authcToken.getUsername());
		session.setAttribute("id", authcToken.getId());
		session.setAttribute("roleType",authcToken.getRoleType());
		session.setAttribute("name", authcToken.getName());
		session.setAttribute("image", authcToken.getImage());
		logger.debug("用户登录成功，并写入session当中");
	}

	@Override
	public void onFailure(AuthenticationToken token, AuthenticationException ae) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onLogout(PrincipalCollection principals) {
		// TODO Auto-generated method stub

	}
	private Session getSession() {
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession(true);
		return session;
	}

}
